// components/search/search.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    value: '',
    searchResults: [], // 搜索结果
    showEmptyState: false, // 是否显示空状态
    isSearching: false, // 是否正在搜索
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 输入框值改变时触发
    onChangeValue(e) {
      const value = e.detail.value;
      this.setData({
        value: value
      });
    },

    // 点击搜索按钮时触发
    onSearch() {
      const keyword = this.data.value.trim();
      
      // 如果没有关键词，显示空状态
      if (!keyword) {
        this.setData({
          searchResults: [],
          showEmptyState: true,
          isSearching: false
        });
        return;
      }
      
      // 显示正在搜索状态
      this.setData({
        isSearching: true,
        showEmptyState: false
      });
      
      // 模拟搜索商品（在实际项目中，这里应该调用后端API）
      this.searchProducts(keyword);
    },

    // 模拟搜索商品的方法
    searchProducts(keyword) {
      // 模拟搜索延迟
      setTimeout(() => {
        // 模拟商品数据（在实际项目中，这些数据应该从后端获取）
        const mockProducts = [
          {
            id: "01",
            name: "奈雪畅畅小绿瓶",
            aa1: "每日500蔬果瓶",
            aa2: "轻松顺畅",
            text: "潮汕油柑 | 香蜜杨桃 | 羽衣甘蓝",
            price: 19,
            image: "https://nayukis-tea.oss-cn-hangzhou.aliyuncs.com/dev/static/images/a.jpg"
          },
          {
            id: "02",
            name: "奈雪瘦瘦小绿瓶",
            aa1: "每日500蔬果瓶",
            aa2: "瘦瘦轻盈",
            text: "膳食纤维 | 超级食材羽衣&橙",
            price: 19,
            image: "https://nayukis-tea.oss-cn-hangzhou.aliyuncs.com/dev/static/images/qwe.jpg"
          },
          {
            id: "03",
            name: "滇西树番茄双抗小红瓶",
            aa1: "每日500蔬果瓶",
            aa2: "维C快充",
            text: "云南山野 | 提亮之酸",
            price: 19,
            image: "https://nayukis-tea.oss-cn-hangzhou.aliyuncs.com/dev/static/images/y.jpg"
          },
          {
            id: "04",
            name: "茉莉初雪奶茶",
            aa1: "招牌奶茶",
            aa2: "金奖茶茉莉初雪",
            text: "0奶精 | 0香精 | 鲜果八窨",
            price: 16,
            image: "https://nayukis-tea.oss-cn-hangzhou.aliyuncs.com/dev/static/images/d.jpg"
          },
          {
            id: "05",
            name: "金色山脉珍珠奶茶",
            aa1: "滇红蜜香",
            aa2: "手熬黑糖珍珠",
            text: "古树金芽|红茶雅韵",
            price: 16,
            image: "https://nayukis-tea.oss-cn-hangzhou.aliyuncs.com/dev/static/images/e.jpg"
          }
        ];

        // 过滤商品（模拟搜索逻辑）
        const filteredProducts = mockProducts.filter(product => 
          product.name.includes(keyword) || 
          product.text.includes(keyword) ||
          product.aa1.includes(keyword) ||
          product.aa2.includes(keyword)
        );

        // 按照 order 页面的结构组织数据
        const searchResults = [{
          title: "搜索结果",
          items: filteredProducts
        }];

        // 更新数据
        this.setData({
          searchResults: searchResults,
          showEmptyState: filteredProducts.length === 0,
          isSearching: false
        });
      }, 500); // 模拟网络延迟
    },

    // 跳转到商品详情页
    goToProductDetail(e) {
      const productId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/secondPages/menu/menu?id=${productId}`
      });
    }
  },
})